<div id="container">
<p id="description"></p>
<div id="sample" contenteditable="true">
<ul>
    <li><p>This</p></li>
    <li><p>is</p></li>
    <li><p>a</p></li>
    <li><p>list</p></li>
</ul>
</div>
</div>
<script src="../../resources/js-test.js"></script>
<script>
function $(id) { return document.getElementById(id); }

var actualHTML;
var selection = getSelection();

function testIt(nth, message, expectedText, expectedHTML, initRange, getResult)
{
    var listItem = $('sample').querySelectorAll('li')[nth];
    var range = document.createRange();
    initRange(listItem, range);
    selection.removeAllRanges();
    selection.addRange(range);
    document.execCommand('InsertText', false, '\n');
    debug(message);
    actualHTML = getResult(listItem).outerHTML;
    shouldBeEqualToString('actualHTML', expectedHTML);
    shouldBeEqualToString('selection.type', 'Caret');
    shouldBeEqualToString('selection.focusNode.textContent', expectedText);
    shouldBe('selection.focusOffset', '0');
    debug('\n');
}

$('sample').focus();

testIt(2, 'insert at end', '', '<li><p><br></p></li>',
    function(listChild, range) {
        var textNode = listChild.firstChild.firstChild;
        range.setStart(textNode, textNode.nodeValue.length);
    },
    function(listItem) {
        return listItem.nextElementSibling;
    });

testIt(1, 'insert at start', 'is', '<li><p><br></p></li>',
    function(listChild, range) {
        var textNode = listChild.firstChild.firstChild;
        range.setStart(textNode, 0);
    },
    function(listItem) {
        return listItem.previousElementSibling;
    });

testIt(0, 'insert at middle', 'his', '<li><p>his</p></li>',
    function(listChild, range) {
        var textNode = listChild.firstChild.firstChild;
        range.setStart(textNode, 1);
    },
    function(listItem) {
        return listItem.nextElementSibling;
    });

if (window.testRunner)
    $('container').outerHTML = '';
</script>
